<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>first_of &#8212; Boost.HigherOrderFunctions 0.6 documentation</title>
    
    <link rel="stylesheet" href="../../../_static/boostbook.css" type="text/css" />
    <link rel="stylesheet" href="../../../_static/pygments.css" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../../../',
        VERSION:     '0.6',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true,
        SOURCELINK_SUFFIX: '.txt'
      };
    </script>
    <script type="text/javascript" src="../../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../../_static/doctools.js"></script>
    <link rel="index" title="Index" href="../../../genindex.html" />
    <link rel="search" title="Search" href="../../../search.html" />
    <link rel="next" title="fix" href="fix.html" />
    <link rel="prev" title="decorate" href="decorate.html" /> 
  </head>
  <body role="document">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86"src="../../../_static/boost.png"></td>
</tr></table>

<div class="spirit-nav">
<a accesskey="p"  href="decorate.html"><img src="../../../_static/prev.png" alt="Prev"></a>
<a accesskey="u" href="../../../doc/src/reference.html"><img src="../../../_static/up.png" alt="Up"></a>
<a accesskey="h" href="../../../doc/index.html"><img src="../../../_static/home.png" alt="Home"></a>
<a accesskey="n"  href="fix.html"><img src="../../../_static/next.png" alt="Next"></a>
</div>
  

    <div class="document">
  <div class="chapter">
      <div class="body" role="main">
        
  <div class="section" id="first-of">
<h1>first_of<a class="headerlink" href="#first-of" title="Permalink to this headline">¶</a></h1>
<div class="section" id="header">
<h2>Header<a class="headerlink" href="#header" title="Permalink to this headline">¶</a></h2>
<div class="highlight-cpp"><div class="highlight"><pre><span></span><span class="cp">#include</span> <span class="cpf">&lt;boost/hof/first_of.hpp&gt;</span><span class="cp"></span>
</pre></div>
</div>
</div>
<div class="section" id="description">
<h2>Description<a class="headerlink" href="#description" title="Permalink to this headline">¶</a></h2>
<p>The <code class="docutils literal"><span class="pre">first_of</span></code> function adaptor combines several functions together. If
the first function can not be called, then it will try to call the next
function. This can be very useful when overloading functions using
template constraints(such as with <code class="docutils literal"><span class="pre">enable_if</span></code>).</p>
<p>Note: This is different than the <code class="docutils literal"><span class="pre">match</span></code> function adaptor, which
can lead to ambiguities. Instead, <code class="docutils literal"><span class="pre">first_of</span></code> will call the first function
that is callable, regardless if there is another function that could be
called as well.</p>
</div>
<div class="section" id="synopsis">
<h2>Synopsis<a class="headerlink" href="#synopsis" title="Permalink to this headline">¶</a></h2>
<div class="highlight-cpp"><div class="highlight"><pre><span></span><span class="k">template</span><span class="o">&lt;</span><span class="n">class</span><span class="p">...</span> <span class="n">Fs</span><span class="o">&gt;</span>
<span class="k">constexpr</span> <span class="n">first_of_adaptor</span><span class="o">&lt;</span><span class="n">Fs</span><span class="p">...</span><span class="o">&gt;</span> <span class="n">first_of</span><span class="p">(</span><span class="n">Fs</span><span class="p">...</span> <span class="n">fs</span><span class="p">);</span>
</pre></div>
</div>
</div>
<div class="section" id="requirements">
<h2>Requirements<a class="headerlink" href="#requirements" title="Permalink to this headline">¶</a></h2>
<p>Fs must be:</p>
<ul class="simple">
<li><a class="reference internal" href="../../../doc/src/concepts.html#constinvocable"><span class="std std-ref">ConstInvocable</span></a></li>
<li>MoveConstructible</li>
</ul>
</div>
<div class="section" id="example">
<h2>Example<a class="headerlink" href="#example" title="Permalink to this headline">¶</a></h2>
<div class="highlight-cpp"><div class="highlight"><pre><span></span><span class="cp">#include</span> <span class="cpf">&lt;boost/hof.hpp&gt;</span><span class="cp"></span>
<span class="cp">#include</span> <span class="cpf">&lt;iostream&gt;</span><span class="cp"></span>
<span class="k">using</span> <span class="k">namespace</span> <span class="n">boost</span><span class="o">::</span><span class="n">hof</span><span class="p">;</span>

<span class="k">struct</span> <span class="n">for_ints</span>
<span class="p">{</span>
    <span class="kt">void</span> <span class="k">operator</span><span class="p">()(</span><span class="kt">int</span><span class="p">)</span> <span class="k">const</span>
    <span class="p">{</span>
        <span class="n">printf</span><span class="p">(</span><span class="s">&quot;Int</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">);</span>
    <span class="p">}</span>
<span class="p">};</span>

<span class="k">struct</span> <span class="n">for_floats</span>
<span class="p">{</span>
    <span class="kt">void</span> <span class="k">operator</span><span class="p">()(</span><span class="kt">float</span><span class="p">)</span> <span class="k">const</span>
    <span class="p">{</span>
        <span class="n">printf</span><span class="p">(</span><span class="s">&quot;Float</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">);</span>
    <span class="p">}</span>
<span class="p">};</span>

<span class="kt">int</span> <span class="nf">main</span><span class="p">()</span> <span class="p">{</span>
    <span class="n">first_of</span><span class="p">(</span><span class="n">for_ints</span><span class="p">(),</span> <span class="n">for_floats</span><span class="p">())(</span><span class="mf">3.0</span><span class="p">);</span>
<span class="p">}</span>
</pre></div>
</div>
<p>This will print <code class="docutils literal"><span class="pre">Int</span></code> because the <code class="docutils literal"><span class="pre">for_floats</span></code> function object won&#8217;t ever be
called. Due to the conversion rules in C++, the <code class="docutils literal"><span class="pre">for_ints</span></code> function can be
called on floats, so it is chosen by <code class="docutils literal"><span class="pre">first_of</span></code> first, even though
<code class="docutils literal"><span class="pre">for_floats</span></code> is a better match.</p>
<p>So, the order of the functions in the <code class="docutils literal"><span class="pre">first_of_adaptor</span></code> are very important
to how the function is chosen.</p>
</div>
<div class="section" id="references">
<h2>References<a class="headerlink" href="#references" title="Permalink to this headline">¶</a></h2>
<ul class="simple">
<li><a class="reference external" href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0051r2.pdf">POO51</a> - Proposal for C++
Proposal for C++ generic overload function</li>
<li><a class="reference internal" href="../../../doc/src/example_overloading.html#conditional-overloading"><span class="std std-ref">Conditional overloading</span></a></li>
</ul>
</div>
</div>


      </div>
  </div>
      <div class="clearer"></div>
    </div>
    <div class="footer" role="contentinfo">
    <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
    <td align="left"></td>
    <td align="right"><div class="copyright-footer">
            &#169; Copyright 2016, Paul Fultz II.
        
          Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.5.6.
          <p>Distributed under the Boost Software License, Version 1.0.
          (See accompanying file <code class="filename">LICENSE_1_0.txt</code> or copy at 
          <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
          </p>
    </div></td>
    </tr></table>
    </div>
  </body>
</html>